package m4;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接
        ConnectionFactory f =  new ConnectionFactory();
        f.setHost("wht6.cn");//wht6.cn
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();
        //1创建随机队列 2创建交换机 3绑定
      String queue=  UUID.randomUUID().toString();
      //                        非持久      独占       自动删除
      c.queueDeclare(queue, false, true, true, null);//创建随机队列

        c.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);//创建交换机

        System.out.println("输入绑定键，用空格隔开： ");
       String s= new Scanner(System.in).nextLine();
         String []a =   s.split("\\s+");
        for (String key :a ) {
            c.queueBind(queue, "direct_logs", key);
        }

        //随机队列接收消费数据
        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String s, Delivery message) throws IOException {
                String s1=new String(message.getBody());
                System.out.println("收到： "+s1);

            }
        };
        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };
        c.basicConsume(queue, true, deliverCallback, cancelCallback);
        //
    }
}
